package com.daimakuai.service.impl;

import com.daimakuai.dao.AdminDao;
import com.daimakuai.entity.Admin;
import com.daimakuai.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.support.ManagedList;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author happyonion.yangtao
 * @Title: AdminServiceImpl
 * @ProjectName adminb-ehind
 * @PackageName com.daimakuai.service.impl
 * @Description TODO
 * @date Create at 2018/11/29 22:42
 */

@SuppressWarnings("ALL")
@Service("adminService")
@Transactional(rollbackFor = Exception.class)
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminDao adminDao;
    @Autowired
    private DataSourceTransactionManager transactionManager;

    @Override
    public AdminDao getAdminById(String id) {
        return adminDao.getAdminById(id);
    }

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return adminDao.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Admin record) {
        return adminDao.insert(record);
    }

    @Override
    public int insertSelective(Admin record) {
        return adminDao.insertSelective(record);
    }

    @Override
    public Admin selectByPrimaryKey(Integer id) {
        return adminDao.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(Admin record) {
        return adminDao.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Admin record) {
        return adminDao.updateByPrimaryKey(record);
    }


    @Override
    //@Transactional(isolation = Isolation.READ_COMMITTED,rollbackFor = Exception.class)
    public  List<Admin> listForDirtyRead() {

        List<Admin> admins = adminDao.listForDirtyRead();

        return admins;
}

    @Override
    @Transactional
    public void insertForDirtyReadAndIllusion() {
        //PROPAGATION_REQUIRED ：如果存在一个事务，则支持当前事务。如果没有事务则开启一个新的事务。
        DefaultTransactionDefinition def = new DefaultTransactionDefinition();
        def.setName("SomeTxName");
        // 设置事务传播方式
        def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);

        TransactionStatus status = transactionManager.getTransaction(def);
        System.out.println("===========================7===增加数据...");
        Admin tmp = new Admin();
        tmp.setId(3);
        tmp.setName("b");
        tmp.setEmail("bb@bb.com");
        tmp.setPassword("bb");
        tmp.setBrithday("2018-11-2");
        adminDao.insertSelective(tmp);

        try {
            Thread.sleep(1000 * 30);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("==============================增加数据0000");
        try{
            int a = 1/0;
        }catch (Exception e){
            transactionManager.rollback(status);
        }

    }
}
